using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class mineShop : MonoBehaviour
{
    public Material material_origin;
    private GameObject intergralManageObj;
    private Text shopMany;
    private GameObject mine;
    void Start()
    {
        material_origin = new Material(gameObject.transform.GetComponent<Renderer>().material);
        intergralManageObj = GameObject.Find("intergral");
    }

    // Update is called once per frame
    void Update()
    {
        if (intergralManageObj != null)
        {
            shopMany = intergralManageObj.transform.Find("number").GetComponent<Text>();
        }
    }
    private void OnMouseOver()
    //鼠标悬停时调用
    {
        gameObject.transform.GetComponent<Renderer>().material.color = Color.green;

        //将Render的材质颜色设置为红色     
    }
    private void OnMouseExit()
    //鼠标退出时调用
    {
        gameObject.transform.GetComponent<Renderer>().material.color = material_origin.color;
    }
    IEnumerator OnMouseDown()
    {
        int currMany = int.Parse(shopMany.text);
        if (currMany>500)
        {
            currMany -=500;
            shopMany.text = currMany.ToString();
            mine = Instantiate((GameObject)Resources.Load("mine"));
            //获取鼠标点击位置到相机的y坐标
            float z = Camera.main.WorldToScreenPoint(transform.position).z;
            //计算鼠标点击位置与对象位置的偏移量
            Vector3 offset = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, z)) - transform.position;
            while (Input.GetMouseButton(0))
            {
                //获取当前鼠标位置
                mine.gameObject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, z)) - offset;
                //设置对象位置为当前鼠标位置
                yield return new WaitForFixedUpdate();
            }
        }
    }
}
